#import "SEGProfileModel.h"
#import <objc/runtime.h>

#if __has_include(<SEGPlatform/SEGPlatformManager.h>)
#import <SEGPlatform/SEGPlatformManager.h>
#else
#import "SEGPlatformManager.h"
#endif

#import "SEGProfileManager.h"

@implementation SEGProfileHouselistModel
-(instancetype)init{
    if (self == [super init]) {
        self.houseId = @"";
        self.houseName = @"";
        self.communityId = @"";
        self.communityName = @"";
        self.buildId = @"";
        self.unitId = @"";
        self.roomNo = @"";
        self.custId = @"";
        self.custType = @"";
    }
    return self;
}

- (NSString *)fullHouseName {
    return [NSString stringWithFormat:@"%@%@", (self.communityName ?: @""), (self.houseName ?: @"")];
}

#pragma mark - <NSCoding>
- (void)encodeWithCoder:(NSCoder *)encoder {
    unsigned int count = 0;
    objc_property_t *propertes = class_copyPropertyList([self class], &count);
    for (int i=0; i<count; i++) {
        const char *propertyName = property_getName(propertes[i]);
        NSString *name = [NSString stringWithUTF8String:propertyName];
        id value = [self valueForKey:name];
        [encoder encodeObject:(value)?value:@"" forKey:name];
    }
}

- (id)initWithCoder:(NSCoder *)decoder {
    if (self = [super init]) {
        unsigned int count =0;
        objc_property_t *propertes = class_copyPropertyList([self class], &count);
        for (int i = 0; i < count; i++) {
            const char *propertyName = property_getName(propertes[i]);
            NSString *name = [NSString stringWithUTF8String:propertyName];
            id value = [decoder decodeObjectForKey:name];
            [self setValue:(value)?value:@"" forKey:name];
        }
        
    }
    return self;
}

@end

@implementation SEGProfileModel
-(instancetype)init{
    if (self == [super init]) {
        _region = @"";
        _ico = @"";
        _provinceId = @"";
        _growth = @"";
        _level = @"";
        _lastLoginTime = @"";
        _houseList = [NSArray<SEGProfileHouselistModel*> array];
        _levelUrl = @"";
        _integalValue = @"";
        _jobCommunity = @"";
        _jobName = @"";
        _nickname = @"";
        _custId = @"";
        _tel = @"";
        _type = @"";
        _sendExpressServiceTel = @"";
        _isDoor = @"";
        _subUserId = @"";
        _email = @"";
        _sex = @"";
        _updateTime = @"";
        _communityTagFlag = @"";
        _isCommunitySignExpress = @"";
        _name = @"";
        _communityName = @"";
        _city = @"";
        _status = @"";
        _cityId = @"";
        _province = @"";
        _sign = @"";
        _telTagFlag = @"";
        _job = @"";
        _submitAuthFlag = @"";
        _desc = @"";
        _communityTel = @"";
        _createTime = @"";
        _qq = @"";
        _jobNumber = @"";
        _masterUserId = @"";
        _age = @"";
        _authType = @"";
        _profileFinishFlag = @"";
        _identity = @"";
        _regionId = @"";
        _organId = @"";
        _userId = @"";
        _houseId = @"";
        _houseName = @"";
        _addressId = @"";
        _defaultAddress = @"";
    }
    return self;
}

+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{@"houseList":SEGProfileHouselistModel.class};
}


//返回一个 Dict，将 Model 属性名对映射到 JSON 的 Key。
+ (NSDictionary *)modelCustomPropertyMapper {
    return @{@"desc" : @[@"desc", @"userDesc"]};
}

- (NSString *)defaultCustType {
    NSString *defaultCustType = @"";
    for (SEGProfileHouselistModel *houseModel in self.houseList) {
        if ([houseModel.houseId isEqualToString:self.houseId]) {
            defaultCustType = houseModel.custType;
            break;
        }
    }
    return defaultCustType;
}

#pragma mark - <NSCoding>
- (void)encodeWithCoder:(NSCoder *)encoder
{
    unsigned int count = 0;
    objc_property_t *propertes = class_copyPropertyList([self class], &count);
    for (int i=0; i<count; i++) {
        const char *propertyName = property_getName(propertes[i]);
        NSString *name = [NSString stringWithUTF8String:propertyName];
        if (name && [name isKindOfClass:[NSString class]] && name.length > 0) {
            id value = [self valueForKey:name];
            [encoder encodeObject:(value)?value:@"" forKey:name];
        }
    }
}

- (id)initWithCoder:(NSCoder *)decoder
{
    if (self = [super init]) {
        unsigned int count =0;
        objc_property_t *propertes = class_copyPropertyList([self class], &count);
        for (int i = 0; i < count; i++) {
            const char *propertyName = property_getName(propertes[i]);
            NSString *name = [NSString stringWithUTF8String:propertyName];
            if (name && [name isKindOfClass:[NSString class]] && name.length > 0) {
                id value = [decoder decodeObjectForKey:name];
                [self setValue:(value)?value:@"" forKey:name];
            }
        }
    }
    return self;
}


@end
